package com.web.quartz.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.web.common.basic.quartz.api.QuartzApi;
import com.web.common.basic.quartz.entity.QuartzJobDetail;
import com.web.common.basic.quartz.entity.QuartzJobDetailEntity;
import com.web.common.basic.quartz.entity.QuartzJobExecLog;
import com.web.common.basic.quartz.entity.QuartzJobExecLogEntity;
import com.web.common.basic.quartz.entity.QuartzJobOperateLog;
import com.web.common.basic.quartz.entity.QuartzJobOperateLogEntity;
import com.web.common.basic.quartz.entity.QuartzJobParam;
import com.web.common.result.R;
import com.web.quartz.service.impl.QuartzJobExecLogServiceImpl;
import com.web.quartz.service.impl.QuartzJobServiceImpl;
import com.web.quartz.service.impl.QuartzOperateLogServiceImpl;
import com.web.service.filter.resubmit.Resubmit;
import com.web.service.handler.error.CustomException;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 定时任务名称 前端控制器
 * </p>
 *
 * @author zhouhui
 * @since 2023-08-08
 */
@Tag(name = "定时任务")
@Controller
@RequestMapping("/basic/quartz")
public class QuartzJobController implements QuartzApi {

	@Autowired
	private QuartzJobServiceImpl quartzJobServiceImpl;
	
	@Autowired
	private QuartzJobExecLogServiceImpl quartzJobExecLogServiceImpl;
	
	@Autowired
	private QuartzOperateLogServiceImpl quartzOperateLogServiceImpl;

	@Operation(summary = "获取定时任务列表")
	@Override
	public R<IPage<QuartzJobDetail>> getJobList(QuartzJobDetailEntity condition) {
		return quartzJobServiceImpl.getJobList(condition);
	}
	
	@Operation(summary = "获取定时任务详细信息")
	@Override
	public R<QuartzJobParam> getJobDetail(QuartzJobParam job) {
		if(!StringUtils.hasText(job.getJobName())) {
			throw new CustomException("未选择要执行的定时任务");
		}
		return quartzJobServiceImpl.getJobDetail(job);
	}

	@Resubmit
	@Operation(summary = "保存定时任务")
	@Override
	public R<Object> saveQuartzJob(@Valid QuartzJobParam job) {
		job.setJobGroup(null);//动态添加的定时任务使用默认分组
		return quartzJobServiceImpl.saveQuartzJob(job);
	}

	@Operation(summary = "删除定时任务")
	@Override
	public R<Object> deleteQuartzJob(QuartzJobParam job) {
		return quartzJobServiceImpl.deleteQuartzJob(job);
	}

	@Operation(summary = "停用或者启用定时任务")
	@Override
	public R<Object> runOrStopQuartzJob(QuartzJobParam job) {
		return quartzJobServiceImpl.runOrStopQuartzJob(job);
	}

	@Operation(summary = "执行一次定时任务")
	@Override
	public R<Object> executeOnceQuartzJob(QuartzJobParam job) {
		if(!StringUtils.hasText(job.getJobName())) {
			throw new CustomException("未选择要执行的定时任务");
		}
		return quartzJobServiceImpl.executeOnceQuartzJob(job);
	}

	@Operation(summary = "获取定时任务执行日志记录")
	@Override
	public R<IPage<QuartzJobExecLog>> getExecLogList(QuartzJobExecLogEntity condition) {
		return quartzJobExecLogServiceImpl.getExecLogList(condition);
	}

	@Operation(summary = "获取定时任务操作日志记录")
	@Override
	public R<IPage<QuartzJobOperateLog>> getOperateLogList(QuartzJobOperateLogEntity condition) {
		return quartzOperateLogServiceImpl.getOperateLogList(condition);
	}
}
